package surveybrain.common
{
   import flash.utils.ByteArray;
   import flash.external.ExternalInterface;
   import mx.controls.Alert;
   
   public class Utils
   {
      
     //mcn 12/30/06: sweet. I got the clone code from "http://www.kirupa.com/forum/showthread.php?p=1897368" and then
     //tweaked it slightly.  The original author stated that this code did not maintain the type
     //association.  However, I added the cast and it works fine (maybe since its just a VO w/o any methods?).
      public static function cloneVO(source : Object) : Object
      {
          var copier:ByteArray = new ByteArray();
          copier.writeObject(source);
          copier.position = 0;
          return ( copier.readObject() );
      }         


      public static function truncateString(str : String, maxLength : int) : String
      {
         if ( str.length > maxLength )
         {
            str = str.substr(0, maxLength - 3) + "...";
         }
         
         return str;
      }       
      
      //mcn 8/26/07: Hello from Mackinaw! Lifted below from a blog to return the URL params
      //that where passed on the URL of the html wrapper.
      public static function getUrlParams() : Object
      {

		   var _params:Object;
		   var _queryString : String;
		
			_params = {};
			_queryString = ExternalInterface.call("window.location.search.substring", 1);
			if(_queryString)
			{
				var params:Array = _queryString.split('&');
				var length:uint = params.length;
				
				for (var i:uint=0,index:int=-1; i<length; i++) 
				{
					var kvPair:String = params[i];
					if((index = kvPair.indexOf("=")) > 0)
					{
						var key:String = kvPair.substring(0,index);
						var value:String = kvPair.substring(index+1);
						_params[key] = value;
					}
				}
			}
			
			return _params;
		}


      public static function showRestrictedFuncAlert(maxSurveyResp : String) : void
      {
        var msg : String = "This feature is only available for surveys containing " + maxSurveyResp + " responses or less.\r\r" +
                           "To remove this restriction, please upgrade to a Premium Account via the \"Upgrade\" button located within \"My Account Home\".";
       
        Alert.show(msg, "Premium Account Required");  
		}


		
   }
}